package g

import (
	"context"
	"gitee.com/injoyai/goutil/cfg"
	"gitee.com/injoyai/goutil/conv"
	"gitee.com/injoyai/goutil/net/conn/v2"
	"gitee.com/injoyai/goutil/net/tcp/v2"
	"math/rand"
	"time"
)

var r *rand.Rand

// Rand 随机数
func Rand() *rand.Rand {
	if r == nil {
		r = rand.New(rand.NewSource(time.Now().UnixNano()))
	}
	return r
}

// Cfg 读取配置文件
func Cfg(paths ...string) *cfg.Entity { return cfg.Take(paths...) }

// Ctx context.Background
func Ctx() context.Context { return context.Background() }

// NewDMap 解析到最底层
func NewDMap(v interface{}) *conv.Map { return conv.NewMap(v) }

// NewMap 任意数据解析到map
func NewMap(i interface{}) Map { return newMap(i) }

//===================================

// DialTCP 建立TCP连接
func DialTCP(addr string) (*conn.Conn, error) { return conn.DialTCP(addr) }

//RedialTCP TCP客户端,默认失败无限重试
func RedialTCP(addr string, fn ...func(client *conn.Conn)) *conn.Conn {
	return conn.RedialTCP(addr, fn...)
}

// DialUDP 建立UDP连接
func DialUDP(addr string) (*conn.Conn, error) { return conn.DialUDP(addr) }

//RedialUDP TCP客户端,默认失败无限重试
func RedialUDP(addr string, fn ...func(client *conn.Conn)) *conn.Conn {
	return conn.RedialUDP(addr, fn...)
}

// TCPServer 新建TCP服务端
func TCPServer(port int) (*tcp.Server, error) { return tcp.NewServer(port) }
